<template>
  <div class="cart-sku" ref="target">
    <div class="attrs">
      <span class="ellipsis" @click="toggle">{{attrsText}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="layer" v-if="isShow">
        <template v-if="goods">
            <GoodsSku @change="changeSku" v-if="goods" :skuId="skuId" :goods="goods" />
            <XtxButton @click="submit" v-if="goods" type="primary" size="mini" style="margin-left:60px">确认</XtxButton>
            <XtxButton @click="hide" type="gray" size="mini" style="margin-left:10px">取消</XtxButton>
        </template>
        <div class="loading" v-else></div>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue-demi'
import { onClickOutside } from '@vueuse/core'
import { getSpecsAndSkus } from '@/api/goods'
import GoodsSku from '@/views/goods/components/goods-sku.vue'
import Message from '@/components/library/Message'
export default {
  components: { GoodsSku },
  name: 'CartSku',
  props: {
    attrsText: {
      type: String,
      default: ''
    },
    skuId: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    // 显示隐藏切换效果
    const isShow = ref(false)
    const goods = ref(null)
    const show = async () => {
      const { result } = await getSpecsAndSkus(props.skuId)
      goods.value = result
      isShow.value = true
    }
    const hide = () => {
      goods.value = ''
      isShow.value = false
    }
    const toggle = () => {
      isShow.value ? hide() : show()
    }

    // 点击元素外隐藏弹出层
    const target = ref(null)
    onClickOutside(target, () => {
      hide()
    })

    // 商品规格的skuId发生改变时
    const currSku = ref(null)
    const changeSku = (val) => {
      currSku.value = val
    }

    const submit = () => {
      // 如果没有选择新的规格，直接隐藏不处理
      if (!currSku.value) return hide()
      // 如果有空对象，但是没有 skuId ，轻提示警告
      if (!currSku.value.skuId) return Message({ message: '请选择完整规格' })
      // 如果选择的规格和以前的一样，直接隐藏不处理
      if (currSku.value.skuId === props.skuId) return hide()
      // 选择了新的规格，通知父组件
      if (currSku.value.skuId) {
        emit('change', currSku.value)
        hide()
      }
    }

    return { isShow, toggle, target, goods, changeSku, submit, hide }
  }
}
</script>
<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display:inline-block;
  .attrs {
    line-height: 24px;
    display: flex;
    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }
    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }
  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor,50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;
    &::before {
      content: "";
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(.8,1) rotate(45deg);
    }
    .loading {
      height: 224px;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
